<?php

/**
* This function will display a list of all communities in the system, broken into members and non-members...
*  It was created to make it easier for students to find their courses for Fall 2006 semester
*
* @package folio
* @param int $user_id Required, the current user id value.
* @param string OUTPUT html code
*/
function communitylist($user_id) {
	// This could be done much more efficiently in a sql subquery.  However, to make it 
	// 	compatable with the older version of godaddy that we're using, I iterate thru
	//	and build thru code.
	global $CFG;
	
	// Validate input
	if ( ! (intval($user_id) > 0 ) ) {
		return '';
	}
	
	// Given a user ID as a parameter, will display a list of communities
    $communities = get_records_sql('SELECT u.* FROM '.$CFG->prefix.'users u
		WHERE u.user_type = ?',
	    array('community'));
    $permissions = get_records_sql('SELECT f.* FROM '.$CFG->prefix.'friends f
		WHERE f.owner = ?',
	    array($user_id));
    
	$joinable = array();
	$joined = array();

	// Separate into two groups
	if ( $communities ) {
		foreach ($communities as $community) {
			$bit = false;
			if ( $permissions ) {
				foreach ( $permissions as $permission ) {
					if ( $permission->friend == $community->ident ) {
						// Add to joined
						$joined[] = $community;
						$bit = true;
						break;
					}
				}
			}
			if ( ! $bit ) {
				// Add to joinable
				$joinable[] = $community;
			}
		}
	}
		
	// ADD THE JOINED COMMUNITIES
    $body = <<< END
    <div class="networktable">
	<h1>Joined Communities</h1>
    <table>
        <tr>
END;
    $i = 1;
    if (!empty($joined)) {
        foreach($joined as $key => $info) {
            $w = 100;
            if (sizeof($joined) > 4) {
                $w = 50;
            }
            // $friends_name = htmlspecialchars(stripslashes($info->name), ENT_COMPAT, 'utf-8');
            $friends_name = run("profile:display:name",$info->ident);
            $info->icon = run("icons:get",$info->ident);
            $friends_menu = run("users:infobox:menu",array($info->ident));
            $body .= <<< END
                <td>
                    <p>
                    <a href="{$CFG->wwwroot}{$info->username}/">
                    <img src="{$CFG->wwwroot}{$info->username}/icons/{$info->icon}/w/{$w}" alt="{$friends_name}" border="0" /></a><br />
                    <span class="userdetails">
                        {$friends_name}
                        {$friends_menu}
                    </span>
                    </p>
                </td>
END;
            if ($i % 5 == 0) {
                $body .= "</tr><tr>";
            }
            $i++;
        }
    } else {
		$body .= "<td><p>No joined communities</p></td>";
    }
    $body .= <<< END
        </tr>
    </table>
    </div>
END;



	// ADD THE JOINABLE COMMUNITIES
    $body .= <<< END
    <div class="networktable">
	<h1>Available Communities</h1>
    <table>
        <tr>
END;
    $i = 1;
    if (!empty($joinable)) {
        foreach($joinable as $key => $info) {
            $w = 100;
            if (sizeof($joinable) > 4) {
                $w = 50;
            }
            // $friends_name = htmlspecialchars(stripslashes($info->name), ENT_COMPAT, 'utf-8');
            $friends_name = run("profile:display:name",$info->ident);
            $info->icon = run("icons:get",$info->ident);
            $friends_menu = run("users:infobox:menu",array($info->ident));
            $body .= <<< END
                <td>
                    <p>
                    <a href="{$CFG->wwwroot}{$info->username}/">
                    <img src="{$CFG->wwwroot}{$info->username}/icons/{$info->icon}/w/{$w}" alt="{$friends_name}" border="0" /></a><br />
                    <span class="userdetails">
                        {$friends_name}
                        {$friends_menu}
                    </span>
                    </p>
                </td>
END;
            if ($i % 5 == 0) {
                $body .= "</tr><tr>";
            }
            $i++;
        }
    } else {
		$body .= "<td><p>No unjoined communities</p></td>";
    }
    $body .= <<< END
        </tr>
    </table>
    </div>
END;
    
    return $body;
    
}

?>